import {defineStore} from 'pinia';
import myAxios from "@/axios/axios.js";

export const useCollectStore = defineStore('collect', {
    state: () => ({
        collectMap: new Map(),     // 存储帖子ID和收藏状态的映射
        collectCountMap: new Map()  // 存储帖子ID和收藏数量的映射
    }),
    actions: {
        /**
         * 收藏
         * @param {string} postId 帖子ID
         * @returns {Promise<boolean>} 操作结果
         */
        async collect(postId) {
            try {
                const res = await myAxios.post(`/collection/${postId}`);
                if (res.code === 0) {
                    this.collectMap.set(postId, true);
                    this.collectCountMap.set(postId, (this.collectCountMap.get(postId) || 0) + 1);
                    return true;
                }
                return false;
            } catch (error) {
                console.error('收藏失败', error);
                return false;
            }
        },

        /**
         * 取消收藏
         * @param {string} postId 帖子ID
         * @returns {Promise<boolean>} 操作结果
         */
        async unCollect(postId) {
            try {
                const res = await myAxios.delete(`/collection/${postId}`);
                if (res.code === 0) {
                    this.collectMap.set(postId, false);
                    this.collectCountMap.set(postId, Math.max(0, (this.collectCountMap.get(postId) || 0) - 1));
                    return true;
                }
                return false;
            } catch (error) {
                console.error('取消收藏失败', error);
                return false;
            }
        },

        /**
         * 获取收藏数量
         * @param {string} postId 帖子ID
         * @returns {Promise<number>} 收藏数量
         */
        async getCollectCount(postId) {
            if (this.collectCountMap.has(postId)) {
                return this.collectCountMap.get(postId);
            }
            try {
                const res = await myAxios.get(`/collection/count/${postId}`);
                if (res.code === 0) {
                    this.collectCountMap.set(postId, res.data);
                    return res.data;
                }
                return 0;
            } catch (error) {
                console.error('获取收藏数量失败', error);
                return 0;
            }
        },

        /**
         * 检查是否已收藏
         * @param {string} postId 帖子ID
         * @returns {Promise<boolean>} 是否已收藏
         */
        async checkHasCollected(postId) {
            if (this.collectMap.has(postId)) {
                return this.collectMap.get(postId);
            }
            try {
                const res = await myAxios.get(`/collection/hasCollected/${postId}`);
                if (res.code === 0) {
                    this.collectMap.set(postId, res.data);
                    return res.data;
                }
                return false;
            } catch (error) {
                console.error('检查是否已收藏失败', error);
                return false;
            }
        }
    }
});
